﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DevExpress.AgDataGrid.Data;
namespace DevExpress.AgDataGrid.Internal {
	public interface ICommandGrid {
		bool IsValidRowHandle(int handle);
		int GetVisibleIndexByRowHandle(int handle);
		double PanelHeight { get; }
		int TopRow { get; set; }
		int LastVisibleElementIndex { get; }
		double GetRowHeight(int visibleIndex);
	}
	enum OperationType {
		None,
		ScrollUp,
		ScrollDown,
		LastLine
	}
	public class MakeElementVisibleCommand {
		ICommandGrid grid;
		public MakeElementVisibleCommand(ICommandGrid grid) {
			this.grid = grid;
		}
		protected internal ICommandGrid Grid { get { return grid; } }
		void MakeElementVisibleByVisibleIndex(int visibleIndex) {
			if (visibleIndex == DataController.InvalidRow) return;
			if (grid.PanelHeight == 0) return;
			switch (OperationTypeSwitcher(visibleIndex)) {
				case OperationType.ScrollUp:
					ScrollUp(visibleIndex);
					break;
				case OperationType.ScrollDown:
					ScrollDown(visibleIndex);
					break;
				case OperationType.LastLine:
					LastLine(visibleIndex);
					break;
			}
		}
		public void MakeElementVisible(int handle) {
			if (!Grid.IsValidRowHandle(handle)) return;
			int index = Grid.GetVisibleIndexByRowHandle(handle);
			MakeElementVisibleByVisibleIndex(index);
		}
		internal void LastLine(int index) {
			double h = 0;
			for (int i = grid.TopRow; i <= index; i++) {
				h += grid.GetRowHeight(i);
			}
			if (h > grid.PanelHeight) {
				ScrollDown(index);
			}
		}
		internal void ScrollDown(int index) {
			int diff = index - grid.LastVisibleElementIndex;
			grid.TopRow += diff == 0 ? 1 : diff;
			MakeElementVisibleByVisibleIndex(index);
		}
		internal void ScrollUp(int index) {
			grid.TopRow = index;
		}
		internal OperationType OperationTypeSwitcher(int visibleIndex) {
			if (visibleIndex < grid.TopRow) {
				return OperationType.ScrollUp;
			}
			if (visibleIndex > grid.LastVisibleElementIndex) {
				return OperationType.ScrollDown;
			}
			if (visibleIndex == grid.LastVisibleElementIndex) {
				return OperationType.LastLine;
			}
			return OperationType.None;
		}
	}
}
